$(function () {
    $("#jqGrid").jqGrid({
        url: '../datacontract/listXtd',
        datatype: "json",
        colModel: [			
			
			{ label: '合同编号', name: 'contractno',index: 'contractno',frozen : true, width: 100 }, 			
			{ label: '合同名称', name: 'contractname',index: 'contractname',frozen : true, width: 150 }, 
			{ label: '签约日期', name: 'contractdate',index: 'contractdate',frozen : true, width: 100,formatter: function(value, options, row){
				return value === null ? 
						'' : 
							value.substring(0,10);
				} }, 			
			{ label: '合同类型', name: 'contracttypeid',index: 'contracttypeid',frozen : true, width: 80,hidden:true }, 			
			{ label: '房源地址', name: 'address',index: 'address',frozen : true, width: 250 }, 			
			{ label: '合同金额(元)', name: 'contractmoney',index: 'contractmoney',frozen : true,formatter: function(value, options, row){
				if(value==0){
					return 0;
				}else if(value!=null){
					return formatCurrency(value)
				}else{
					return "";
				}									
			}, width: 100 }, 			
			{ label: '已付金额(元)', name: 'haspaidmoney',index: 'haspaidmoney',frozen : true,formatter: function(value, options, row){
				if(value==0){
					return 0;
				}else if(value!=null){
					return formatCurrency(value)
				}else{
					return "";
				}									
			}, width: 100 }, 			
			{ label: '未付金额(元)', name: 'nopaidmoney', index: 'nopaidmoney',formatter: function(value, options, row){
				if(value==0){
					return 0;
				}else if(value!=null){
					return formatCurrency(value)
				}else{
					return "";
				}									
			},width: 100 }, 			
			{ label: '合同签约人ID', name: 'employeeid',hidden:true, width: 80 }, 			
			{ label: '分成人ID列表，中间用,隔开', name: 'splitemployeeidlist',hidden:true, width: 80 }, 			
			{ label: '多人分层比例', name: 'splitpercent', width: 80,hidden:true, }, 		
			{ label: '主管编号，用逗号隔开', name: 'directorno',hidden:true, width: 80 }, 			
			{ label: '分店编号，用逗号隔开', name: 'storeno',hidden:true, width: 80 }, 			
			{ label: '区域编号，用逗号隔开', name: 'areano',hidden:true, width: 80 }, 			
			{ label: '大区编号，用逗号隔开', name: 'bigareano',hidden:true, width: 80 }, 			
			{ label: '城市编号，用逗号隔开', name: 'cityno', width: 80,hidden:true },
			{ label: 'id', name: 'id',hidden:true, width: 50, key: true },
			{ label: '合同年月', name: 'contractmonth',hidden:true, width: 80 },
			{ label: '签单人姓名', name: 'employeeName', width: 100 }, 			
			{ label: '分成人工号', name: 'splitEmployeeWorkNoList', width: 80,hidden:true },
			{ label: '分成人姓名', name: 'splitEmployeeNameList', width: 80,hidden:true },
			{ label: '分成比例', name: 'splitpercent',hidden:true, width: 50 },
			{ label: '合同个数', name: 'everyoneNo',hidden:true, width: 80 },
			{ label: '备注', name: 'memo', width: 250 },
			//1
			/*{ label: '分成人工号', name: '', width: 100,formatter: function(value, options, row){
				if(row.splitEmployeeWorkNoList==null || row.splitEmployeeWorkNoList.length==0){
					return "";
				}else{
					if(row.splitEmployeeWorkNoList.indexOf(",")<0){
						return row.splitEmployeeWorkNoList;
					}else{
						if (row.splitEmployeeWorkNoList.split(",")[0]==null) {
							return "";
						} else {
							return row.splitEmployeeWorkNoList.split(",")[0];
						}
					}
				}
			} },
			{ label: '分成人姓名', name: '', width: 100,formatter: function(value, options, row){
				if(row.splitEmployeeNameList==null || row.splitEmployeeNameList.length==0){
					return "";
				}else{
					if(row.splitEmployeeNameList.indexOf(",")<0){
						return row.splitEmployeeNameList;
					}else{
						if (row.splitEmployeeNameList.split(",")[0]==null) {
							return "";
						} else {
							return row.splitEmployeeNameList.split(",")[0];
						}
					}
				}
			} },
			{ label: '分成比例', name: '', width: 80,formatter: function(value, options, row){
				if(row.splitpercent==null || row.splitpercent.length==0){
					return "";
				}else{
					if(row.splitpercent.indexOf(",")<0){
						return row.splitpercent;
					}else{
						if (row.splitpercent.split(",")[0]==null) {
							return "";
						} else {
							return row.splitpercent.split(",")[0];
						}
					}
				}
			}  },
			{ label: '合同个数', name: '',width: 100,formatter: function(value, options, row){
				if(row.everyoneNo==null || row.everyoneNo.length==0){
					return "";
				}else{
					if(row.everyoneNo.indexOf(",")<0){
						return row.everyoneNo;
					}else{
						if (row.everyoneNo.split(",")[0]==null) {
							return "";
						} else {
							return row.everyoneNo.split(",")[0];
						}
					}
				}
			}  },
			
			
			//2
			{ label: '分成人工号', name: '', width: 100,formatter: function(value, options, row){
				if(row.splitEmployeeWorkNoList==null || row.splitEmployeeWorkNoList.length==0){
					return "";
				}else{
					if(row.splitEmployeeWorkNoList.indexOf(",")<0){
						return row.splitEmployeeWorkNoList;
					}else{
						if (row.splitEmployeeWorkNoList.split(",")[1]==null) {
							return "";
						} else {
							return row.splitEmployeeWorkNoList.split(",")[1];
						}
					}
				}
			} },
			{ label: '分成人姓名', name: '', width: 100,formatter: function(value, options, row){
				if(row.splitEmployeeNameList==null || row.splitEmployeeNameList.length==0){
					return "";
				}else{
					if(row.splitEmployeeNameList.indexOf(",")<0){
						return row.splitEmployeeNameList;
					}else{
						if (row.splitEmployeeNameList.split(",")[1]==null) {
							return "";
						} else {
							return row.splitEmployeeNameList.split(",")[1];
						}
					}
				}
			} },
			{ label: '分成比例', name: '', width: 80,formatter: function(value, options, row){
				if(row.splitpercent==null || row.splitpercent.length==0){
					return "";
				}else{
					if(row.splitpercent.indexOf(",")<0){
						return row.splitpercent;
					}else{
						if (row.splitpercent.split(",")[1]==null) {
							return "";
						} else {
							return row.splitpercent.split(",")[1];
						}
					}
				}
			}  },
			{ label: '合同个数', name: '',width: 100,formatter: function(value, options, row){
				if(row.everyoneNo==null || row.everyoneNo.length==0){
					return "";
				}else{
					if(row.everyoneNo.indexOf(",")<0){
						return row.everyoneNo;
					}else{
						if (row.everyoneNo.split(",")[1]==null) {
							return "";
						} else {
							return row.everyoneNo.split(",")[1];
						}
					}
				}
			}  },
			
			
			
			//3
			{ label: '分成人工号', name: '', width: 100,formatter: function(value, options, row){
				if(row.splitEmployeeWorkNoList==null || row.splitEmployeeWorkNoList.length==0){
					return "";
				}else{
					if(row.splitEmployeeWorkNoList.indexOf(",")<0){
						return row.splitEmployeeWorkNoList;
					}else{
						if (row.splitEmployeeWorkNoList.split(",")[2]==null) {
							return "";
						} else {
							return row.splitEmployeeWorkNoList.split(",")[2];
						}
					}
				}
			} },
			{ label: '分成人姓名', name: '', width: 100,formatter: function(value, options, row){
				if(row.splitEmployeeNameList==null || row.splitEmployeeNameList.length==0){
					return "";
				}else{
					if(row.splitEmployeeNameList.indexOf(",")<0){
						return row.splitEmployeeNameList;
					}else{
						if (row.splitEmployeeNameList.split(",")[2]==null) {
							return "";
						} else {
							return row.splitEmployeeNameList.split(",")[2];
						}
					}
				}
			} },
			{ label: '分成比例', name: '', width: 80,formatter: function(value, options, row){
				if(row.splitpercent==null || row.splitpercent.length==0){
					return "";
				}else{
					if(row.splitpercent.indexOf(",")<0){
						return row.splitpercent;
					}else{
						if (row.splitpercent.split(",")[2]==null) {
							return "";
						} else {
							return row.splitpercent.split(",")[2];
						}
					}
				}
			}  },
			{ label: '合同个数', name: '',width: 100,formatter: function(value, options, row){
				if(row.everyoneNo==null || row.everyoneNo.length==0){
					return "";
				}else{
					if(row.everyoneNo.indexOf(",")<0){
						return row.everyoneNo;
					}else{
						if (row.everyoneNo.split(",")[2]==null) {
							return "";
						} else {
							return row.everyoneNo.split(",")[2];
						}
					}
				}
			}  },
			
			
			
			//4
			{ label: '分成人工号', name: '', width: 100,formatter: function(value, options, row){
				if(row.splitEmployeeWorkNoList==null || row.splitEmployeeWorkNoList.length==0){
					return "";
				}else{
					if(row.splitEmployeeWorkNoList.indexOf(",")<0){
						return row.splitEmployeeWorkNoList;
					}else{
						if (row.splitEmployeeWorkNoList.split(",")[3]==null) {
							return "";
						} else {
							return row.splitEmployeeWorkNoList.split(",")[3];
						}
					}
				}
			} },
			{ label: '分成人姓名', name: '', width: 100,formatter: function(value, options, row){
				if(row.splitEmployeeNameList==null || row.splitEmployeeNameList.length==0){
					return "";
				}else{
					if(row.splitEmployeeNameList.indexOf(",")<0){
						return row.splitEmployeeNameList;
					}else{
						if (row.splitEmployeeNameList.split(",")[3]==null) {
							return "";
						} else {
							return row.splitEmployeeNameList.split(",")[3];
						}
					}
				}
			} },
			{ label: '分成比例', name: '', width: 80,formatter: function(value, options, row){
				if(row.splitpercent==null || row.splitpercent.length==0){
					return "";
				}else{
					if(row.splitpercent.indexOf(",")<0){
						return row.splitpercent;
					}else{
						if (row.splitpercent.split(",")[3]==null) {
							return "";
						} else {
							return row.splitpercent.split(",")[3];
						}
					}
				}
			}  },
			{ label: '合同个数', name: '',width: 100,formatter: function(value, options, row){
				if(row.everyoneNo==null || row.everyoneNo.length==0){
					return "";
				}else{
					if(row.everyoneNo.indexOf(",")<0){
						return row.everyoneNo;
					}else{
						if (row.everyoneNo.split(",")[3]==null) {
							return "";
						} else {
							return row.everyoneNo.split(",")[3];
						}
					}
				}
			}  },
			
			
			
			
			//5
			{ label: '分成人工号', name: '', width: 100,formatter: function(value, options, row){
				if(row.splitEmployeeWorkNoList==null || row.splitEmployeeWorkNoList.length==0){
					return "";
				}else{
					if(row.splitEmployeeWorkNoList.indexOf(",")<0){
						return row.splitEmployeeWorkNoList;
					}else{
						if (row.splitEmployeeWorkNoList.split(",")[4]==null) {
							return "";
						} else {
							return row.splitEmployeeWorkNoList.split(",")[4];
						}
					}
				}
			} },
			{ label: '分成人姓名', name: '', width: 100,formatter: function(value, options, row){
				if(row.splitEmployeeNameList==null || row.splitEmployeeNameList.length==0){
					return "";
				}else{
					if(row.splitEmployeeNameList.indexOf(",")<0){
						return row.splitEmployeeNameList;
					}else{
						if (row.splitEmployeeNameList.split(",")[4]==null) {
							return "";
						} else {
							return row.splitEmployeeNameList.split(",")[4];
						}
					}
				}
			} },
			{ label: '分成比例', name: '', width: 80,formatter: function(value, options, row){
				if(row.splitpercent==null || row.splitpercent.length==0){
					return "";
				}else{
					if(row.splitpercent.indexOf(",")<0){
						return row.splitpercent;
					}else{
						if (row.splitpercent.split(",")[4]==null) {
							return "";
						} else {
							return row.splitpercent.split(",")[4];
						}
					}
				}
			}  },
			{ label: '合同个数', name: '',width: 100,formatter: function(value, options, row){
				if(row.everyoneNo==null || row.everyoneNo.length==0){
					return "";
				}else{
					if(row.everyoneNo.indexOf(",")<0){
						return row.everyoneNo;
					}else{
						if (row.everyoneNo.split(",")[4]==null) {
							return "";
						} else {
							return row.everyoneNo.split(",")[4];
						}
					}
				}
			}  },
			
			
			
			//6
			{ label: '分成人工号', name: '', width: 100,formatter: function(value, options, row){
				if(row.splitEmployeeWorkNoList==null || row.splitEmployeeWorkNoList.length==0){
					return "";
				}else{
					if(row.splitEmployeeWorkNoList.indexOf(",")<0){
						return row.splitEmployeeWorkNoList;
					}else{
						if (row.splitEmployeeWorkNoList.split(",")[5]==null) {
							return "";
						} else {
							return row.splitEmployeeWorkNoList.split(",")[5];
						}
					}
				}
			} },
			{ label: '分成人姓名', name: '', width: 100,formatter: function(value, options, row){
				if(row.splitEmployeeNameList==null || row.splitEmployeeNameList.length==0){
					return "";
				}else{
					if(row.splitEmployeeNameList.indexOf(",")<0){
						return row.splitEmployeeNameList;
					}else{
						if (row.splitEmployeeNameList.split(",")[5]==null) {
							return "";
						} else {
							return row.splitEmployeeNameList.split(",")[5];
						}
					}
				}
			} },
			{ label: '分成比例', name: '', width: 80,formatter: function(value, options, row){
				if(row.splitpercent==null || row.splitpercent.length==0){
					return "";
				}else{
					if(row.splitpercent.indexOf(",")<0){
						return row.splitpercent;
					}else{
						if (row.splitpercent.split(",")[5]==null) {
							return "";
						} else {
							return row.splitpercent.split(",")[5];
						}
					}
				}
			}  },
			{ label: '合同个数', name: '',width: 100,formatter: function(value, options, row){
				if(row.everyoneNo==null || row.everyoneNo.length==0){
					return "";
				}else{
					if(row.everyoneNo.indexOf(",")<0){
						return row.everyoneNo;
					}else{
						if (row.everyoneNo.split(",")[5]==null) {
							return "";
						} else {
							return row.everyoneNo.split(",")[5];
						}
					}
				}
			}  },*/
        ],
		viewrecords: true,
        height: $(window).height()-120,
        rowNum: 15,
		rowList : [15,30,50],
        rownumbers: true, 
        rownumWidth: 30, 
        width:window.screen.availWidth-250,  
        autowidth: false,  
        shrinkToFit: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            sort:"sort",//需要排序的列
            order: "order"//asc/desc
        },
        ondblClickRow:function(row){  
            var rowData = $('#jqGrid').jqGrid('getRowData',row);  
            look(rowData.id);  
        }, 
        gridComplete:function(){
        	$('.ui-jqgrid-bdiv').scrollTop(0);
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    jQuery("#jqGrid").jqGrid('setFrozenColumns');
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		value:null,
		calTime:null,
		dataContract: {}
	},
	methods: {
		query: function () {
			vm.calTime=$("#calTime").val();
			vm.reload();
		},
		imp: function(){      
		    //检验导入的文件是否为Excel文件     
		    	$("#uploadFile").click();		    		
		    	$("#sure").show(); 		   
		} ,
		up: function () {
			$("#sure").hide();
			var uploadFile = document.getElementById("uploadFile").value;      
	    	if(uploadFile == null || uploadFile == ''){      
	    		alert("请选择要上传的Excel文件");      
	    		return;      
	    	}else{      
	    		var fileExtend = uploadFile.substring(uploadFile.lastIndexOf('.')).toLowerCase();       
	    		if(fileExtend == '.xls'||fileExtend == '.xlsx'){    
	    			    var formData = new FormData();
	    			    var name = $("#uploadFile").val();
	    			    formData.append("file",$("#uploadFile")[0].files[0]);
	    			    formData.append("name",name);
	    			    $.ajax({
	    			        url : '../datacontract/upload',
	    			        type : 'POST',
	    			        async : false,
	    			        data : formData,
	    			        // 告诉jQuery不要去处理发送的数据
	    			        processData : false,
	    			        // 告诉jQuery不要去设置Content-Type请求头
	    			        contentType : false,
	    			        beforeSend:function(){
	    			            console.log("正在进行，请稍候");
	    			        },
	    			        success : function(responseStr) {
	    			            if(responseStr==20){
	    			                alert("导入成功");
	    			                vm.reload();
	    			            }else{
	    			            	alert("导入失败");
	    			            }
	    			        }
	    			    });
	    		}else{      
	    			alert("文件格式需为'.xls'格式");      
	    			return;      
	    		}      
	    	}      
	    	//提交表单       
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.dataContract = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.dataContract.id == null ? "../datacontract/save" : "../datacontract/update";
			$.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.dataContract),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../datacontract/delete",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../datacontract/info/"+id, function(r){
                vm.dataContract = r.dataContract;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
				postData:{'value': vm.value,"calTime":vm.calTime},
                page:page
            }).trigger("reloadGrid");
		},
		calculate:function(){
			vm.paytime=$("#paytime").val();
			var time=vm.paytime;
			$.post("../datacontract/calculate/"+time, function(r){
                alert(r.mssage)
            });
		}
	}
});
function look(id) {
	top.layer.open({
		type : 2,
		title : '合同详情',
		area : [ '80%', '90%' ],
		shadeClose : false,
		content : 'zjdc/datacontractpayXtd.html?id=' +id,
		btn : [ '确认', '取消' ],
		yes : function(index, layero) {
			top.layer.close(index);
		}
	});
}
function formatCurrency(num) 
{
	if(num)
	{
		//将num中的$,去掉，将num变成一个纯粹的数据格式字符串
		num = num.toString().replace(/\$|\,/g,'');
		//如果num不是数字，则将num置0，并返回
		if(''==num || isNaN(num)){return 'Not a Number ! ';}
		//如果num是负数，则获取她的符号
		var sign = num.indexOf("-")> 0 ? '-' : '';
		//如果存在小数点，则获取数字的小数部分
		var cents = num.indexOf(".")> 0 ? num.substr(num.indexOf(".")) : '';
		cents = cents.length>1 ? cents : '' ;//注意：这里如果是使用change方法不断的调用，小数是输入不了的
		//获取数字的整数数部分
		num = num.indexOf(".")>0 ? num.substring(0,(num.indexOf("."))) : num ;
		//如果没有小数点，整数部分不能以0开头
		if('' == cents){ if(num.length>1 && '0' == num.substr(0,1)){return 'Not a Number ! ';}}
		//如果有小数点，且整数的部分的长度大于1，则整数部分不能以0开头
		else{if(num.length>1 && '0' == num.substr(0,1)){return 'Not a Number ! ';}}
		//针对整数部分进行格式化处理，这是此方法的核心，也是稍难理解的一个地方，逆向的来思考或者采用简单的事例来实现就容易多了
		/*
               也可以这样想象，现在有一串数字字符串在你面前，如果让你给他家千分位的逗号的话，你是怎么来思考和操作的?
               字符串长度为0/1/2/3时都不用添加
               字符串长度大于3的时候，从右往左数，有三位字符就加一个逗号，然后继续往前数，直到不到往前数少于三位字符为止
		 */
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		{
			num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
		}
		//将数据（符号、整数部分、小数部分）整体组合返回
		return (sign + num + cents);    
	}

}